<template>
  <div class="index">
    <div class="context">
      <h2>后台管理系统</h2>
      <div class="text">
        <div class="innerBox">
          <el-input class="input" v-model="userName" placeholder="请输入用户名"></el-input>
          <el-input class="input" placeholder="请输入密码" v-model="password" show-password></el-input>
          <el-input class="input" placeholder="请在次输入密码" v-model="rePassword" show-password></el-input>
        </div>
        <el-button type="primary" class="btn" @click="sendMsg">注册</el-button>
      </div>
      <router-link to="/login">
        <el-link type="info" class="loginBtn">去登录</el-link>
      </router-link>
    </div>
  </div>
</template>

<script>
import { getUserMsg } from '../../api/index'

export default {
  name: 'my-register',
  data () {
    return {
      userName: '',
      password: '',
      rePassword: ''
    }
  },
  methods: {
    async sendMsg () {
      const data = {
        username: this.userName,
        password: this.password,
        repassword: this.rePassword
      }
      const res = await getUserMsg(data)
      console.log(res)
      if (res.data.code === 0) {
        await this.$router.push('/login')
      } else if (res.data.code === 1) {
        await window.alert('用户名被占用，请更换其他用户名！')
      } else {
        await window.alert('注册失败')
      }
    }
  }
}
</script>

<style scoped>
*{
  margin: 0;
  padding: 0;
}
.index{
  height: 100%;
  width: 100%;
  background: url('../../assets/images/login_bg.jpg') no-repeat;
  overflow: hidden;
  background-size: cover;
}
.context{
  width: 400px;
  height: 300px;
  margin: 310px auto;
  background-color: white;
  border-radius: 3px;
}
.context h2{
  text-align: center;
  color: deepskyblue;
  line-height: 50px;
}
.text{
  width: 100%;
  height: 230px;
  position: relative;
}
.btn{
  width: 370px;
  height: 40px;
  position: absolute;
  bottom: 20px;
  left: 15px;
}
.innerBox{
  position: absolute;
  left: 15px;
}
.input{
  width: 370px;
  margin-top: 10px;
}
.loginBtn{
  position: relative;
  left: 20px;
  bottom: 20px;
  font-size: 15px;
}
</style>
